package com.ynet.middleground.user.model;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynet.core.common.BaseReqObj;
import com.ynet.middleground.user.bean.CloudPlatfromCitySearchReq;
import com.ynet.middleground.user.dao.CloudPlatformRegionMapper;
import com.ynet.middleground.user.dto.CloudPlatformCityDTO;
import com.ynet.middleground.user.dto.CloudPlatformProvinceDTO;
import com.ynet.middleground.user.entity.CloudPlatformRegion;
import com.ynet.middleground.user.utils.BeanUtil;
import com.ynet.middleground.user.utils.CloudPlatformUtil;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class CloudPlatformModel  extends ServiceImpl<CloudPlatformRegionMapper, CloudPlatformRegion> {

    @Autowired
    CloudPlatformUtil cloudPlatformUtil;

    @Autowired
    private Mapper dozerBeanMapper;

    //查询云平台省市
    public List<CloudPlatformProvinceDTO> searchAllProvince(BaseReqObj baseReqObj) {
        List<CloudPlatformProvinceDTO> provinceList = new ArrayList<>();
        provinceList = cloudPlatformUtil.searchAllProvince(baseReqObj);
        if(provinceList.size()>0){
            List<CloudPlatformRegion> regionList = new ArrayList<>();
            regionList = BeanUtil.mapList(dozerBeanMapper, provinceList, CloudPlatformRegion.class);
            for(int i=0;i<regionList.size();i++){
                regionList.get(i).setCreateBy(baseReqObj.getOperationUserId());
                regionList.get(i).setGmtCreate(LocalDateTime.now());
            }
            this.saveBatch(regionList);
        }
        return provinceList;
    }

    //根据省代码查询云平台地市
    public List<CloudPlatformCityDTO> searchCityByProvinceCode(CloudPlatfromCitySearchReq reqObj) {
        List<CloudPlatformCityDTO> cityList = new ArrayList<>();
        cityList = cloudPlatformUtil.searchCityByProvinceCode(reqObj);
        if(cityList.size()>0){
            List<CloudPlatformRegion> regionList = new ArrayList<>();
            regionList = BeanUtil.mapList(dozerBeanMapper, cityList, CloudPlatformRegion.class);
            for(int i=0;i<regionList.size();i++){
                regionList.get(i).setCreateBy(reqObj.getOperationUserId());
                regionList.get(i).setGmtCreate(LocalDateTime.now());
            }
            this.saveBatch(regionList);
        }
        return cityList;
    }
}
